package com.nanachi.feign.core;

import com.nanachi.feign.core.context.SimpleFeignContext;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @author yuhongtai
 * feignClient的工厂类
 */
@Data
public class SimpleFeignClientFactoryBean implements FactoryBean<Object>,
        InitializingBean, ApplicationContextAware, BeanFactoryAware {

    private Class<?> type;

    private String name;

    private String path;

    private ApplicationContext applicationContext;

    private BeanFactory beanFactory;

    @Override
    public void setBeanFactory(@NotNull BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }

    @Override
    public Object getObject() {
        return getTarget();
    }

    /**
     * 重点来了，返回真正的代理类
     *
     * @param <T>
     * @return
     */
    private <T> T getTarget() {
        SimpleFeignContext context = beanFactory != null
                ? beanFactory.getBean(SimpleFeignContext.class)
                : applicationContext.getBean(SimpleFeignContext.class);
        //
        String cleanPath = "";
        if(StringUtils.isNotBlank(this.path)){
            if(this.path.startsWith("/")){
                cleanPath = path.substring(1);
            }
            if(this.path.endsWith("/")){
                cleanPath = path.substring(0,path.length()-1);
            }
        }
        SimpleFeign.Builder feign = feign(context);
        feign
    }

    protected SimpleFeign.Builder feign(SimpleFeignContext context){

    }


    @Override
    public Class<?> getObjectType() {
        return null;
    }

    @Override
    public void afterPropertiesSet() throws Exception {

    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
